package com.Dao.impl;

import com.Dao.StudentDao;
import com.domain.Student;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentDaoimpl implements StudentDao {
    @Override
    public List<Student> findAll() {
        //获取sql会话对象
        SqlSession sqlSession = MybatisUtils.getsqlSession();
        //根据会话对象获取映射文件里面的sql语句
        List<Student> students = sqlSession.selectList("StudentMapper.selectAll");
        MybatisUtils.close(sqlSession);
        return students;
    }

    @Override
    public Student findById(Integer id) {
        SqlSession sqlSession = MybatisUtils.getsqlSession();
        Student o = sqlSession.selectOne("StudentMapper.select", id);
        MybatisUtils.close(sqlSession);
        return o;
    }

    @Override
    public int insert(Student stu) {
        SqlSession sqlSession = MybatisUtils.getsqlSession(true);
        int i = sqlSession.insert("StudentMapper.insert", stu);
        MybatisUtils.close(sqlSession);
        return i;
    }

    @Override
    public int update(Student stu) {
        SqlSession sqlSession = MybatisUtils.getsqlSession(true);
        int i = sqlSession.update("StudentMapper.update", stu);
        MybatisUtils.close(sqlSession);
        return i;
    }

    @Override
    public int delete(Integer id) {
        SqlSession sqlSession = MybatisUtils.getsqlSession(true);
        int i = sqlSession.delete("StudentMapper.delete", id);
        MybatisUtils.close(sqlSession);
        return i;
    }
}
